/**
 * scabish前端请求封装框架
 * @requires jQuery
 * @author keluo<pycorvn@yeah.net>
 * @since 2015-02-03
 */
var SCS = {
	
	_debug : {*DEBUG*}, // 是否开启调试模式
	
    _rewrite : {*REWRITE*}, // url是否重写
    
    baseUrl : '{*BASE_URL*}', // url基本地址
    
    /**
     * 创建url
     * @param string route 路由地址，如：api/user
     * @param mixed params key/value键值对，id/1/type/2或{'id':1,'type':2}
     * @return string
     */
    url : function(route, params) {
        if(route.length == 0) route = 'index';
        route = route.split('/');
        var control = route[0];
        var action = (route.length >= 2 && route[1].length) ? route[1] : (params ? 'index' : '');
        route = control+'/'+action;
        var queryString = '';
        if('string' == typeof params) {
            queryString = params;
        } else if('object' == typeof params) {
            for(var key in params) {
                queryString += key+'/'+encodeURIComponent(params[key])+'/';
            }
        }
        var url = this.baseUrl+(this._rewrite ? '/' : '/?')+route+'/'+queryString;
        return url.replace(/\/*$/, '');
    },
    
    /**
     * 解析返回的json数据
     * @param object response
     * @return mixed 解析成功返回数据，失败返回false
     */
    result : function(response) {
        try {
            if(typeof response == 'object' && response.status) {
                return response.data;
            } else {
                return false;
            }
        } catch(e) {
            return false;
        }
    },
    
    error : function(response) {
    	try {
            if(typeof response == 'object' && response.status == 0) {
                return response.data;
            } else {
                return response;
            }
        } catch(e) {
            return response;
        }
    }
};